package team.hyznrj.studentsys.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import team.hyznrj.studentsys.dao.repository.AcademicYearsRepository;
import team.hyznrj.studentsys.dao.repository.CurriculumNatureRepository;
import team.hyznrj.studentsys.dto.CourseDto;
import team.hyznrj.studentsys.entity.AcademicYears;
import team.hyznrj.studentsys.entity.CourseAchievement;
import team.hyznrj.studentsys.entity.CurriculumNature;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ReMidDream
 * @date 2018-03-28 16:25
 **/
@Component
public class CourseAchievement2CourseDto {

    @Autowired
    private CurriculumNatureRepository curriculumNatureRepository;
    @Autowired
    private AcademicYearsRepository academicYearsRepository;

    public CourseAchievement2CourseDto(CurriculumNatureRepository curriculumNatureRepository, AcademicYearsRepository academicYearsRepository) {
        this.curriculumNatureRepository = curriculumNatureRepository;
        this.academicYearsRepository = academicYearsRepository;
    }

    /**
     * 将CourseAchievement转化成CourseDto
     * 实现Service的表连接
     */
    public  List<CourseDto> converter(List<CourseAchievement> courseAchievementList){

        List<Integer> yeas = courseAchievementList.stream().map(CourseAchievement::getAcademicYear).collect(Collectors.toList());
        List<Integer> Natures = courseAchievementList.stream().map(CourseAchievement::getCurriculumNature).collect(Collectors.toList());

        //查询课程性质和学年表
        List<CurriculumNature> curriculumNatureList = curriculumNatureRepository.findAll(Natures);
        List<AcademicYears> academicYearsList = academicYearsRepository.findAll(yeas);

       return courseAchievementList.stream()
                .map(course -> {
                    CourseDto courseDto = new CourseDto();
                    for (CurriculumNature curriculumNature : curriculumNatureList) {
                        if (curriculumNature.getId().equals(course.getCurriculumNature())) {
                            courseDto.setCurriculumNature(curriculumNature.getCurriculumNatureName());
                        }
                    }
                    for (AcademicYears academicYears : academicYearsList) {
                        if (course.getAcademicYear().equals(academicYears.getId())) {
                            courseDto.setAcademicYear(academicYears.getAcademicYear());
                        }
                    }
                    return copy(course,courseDto);
                })
                .collect(Collectors.toList());
    }

    private static CourseDto copy(CourseAchievement courseAchievement,CourseDto courseDto){
        courseDto.setId(courseAchievement.getId());
        courseDto.setStudentId(courseAchievement.getStudentId());
        courseDto.setCourseTitle(courseAchievement.getCourseTitle());
        courseDto.setConvertMark(courseAchievement.getConvertMark());
        courseDto.setCourseGradePoint(courseAchievement.getCourseGradePoint());
        courseDto.setRemarks(courseAchievement.getRemarks());
        courseDto.setCourseCredit(courseAchievement.getCourseCredit());
        courseDto.setSchoolTerm(courseAchievement.getSchoolTerm());
        courseDto.setUserId(courseAchievement.getUserId());
        return courseDto;
    }
}
